//
//  NSString+Extension.m
//  HuaRun2
//
//  Created by admin on 17/8/29.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "NSString+Extension.h"
#import "sys/utsname.h"
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>

@implementation NSString (Extension)

-(CGFloat)heightWithAttributes:(NSDictionary *)attributes width:(CGFloat)awidth{
    
    
    //    计算地址的高度
    CGSize labelSize = CGSizeMake(awidth, MAXFLOAT);
    
    CGFloat addressH = [self boundingRectWithSize:labelSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading                                            attributes:attributes context:nil].size.height+kMargin10;
    
    return addressH;
    
    
}

-(CGFloat)heightWithFontSize:(CGFloat)asize width:(CGFloat)awidth{
    
    //    计算地址的高度
    CGSize labelSize = CGSizeMake(awidth, MAXFLOAT);
    
    CGFloat addressH = [self boundingRectWithSize:labelSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading                                            attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:asize]} context:nil].size.height+kMargin10;
    
    return addressH;
    
}

/** 计算文字的宽度 传字体大小*/
-(CGFloat)WithFontSize:(CGFloat )asize width:(CGFloat)awidth{
    //    计算地址的高度
    CGSize labelSize = CGSizeMake(MAXFLOAT, MAXFLOAT);
    
    CGFloat addressH = [self boundingRectWithSize:labelSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading                                            attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:asize]} context:nil].size.width+kMargin10;
//    NSLog(@"width  = %.f", addressH);
    return addressH;

}

+(BOOL)isemptyStr:(NSString *)str{
    
    
    if (str == nil || str == NULL) {
        
        return YES;
    }
    if ([str isKindOfClass:[NSNull class]]) {
        
        return YES;
    }
    if ([[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        
        return YES;
    }
    return NO;
//    if ([str isEqualToString:@""] ||
//        str.length == 0 ||
//        [str isEqualToString:@"(null)"] ||
//        str == nil) {
//        
//        NSLog(@"---%@-是空字符串",str);
//        
//        return YES;
//    }
//    
//    NSLog(@"---%@-不是空字符串",str);
//    
//    
//    return NO;
}
+ (NSString *)showStringWithStr:(NSString *)str {
    return str ? str : @"";
}

//如果没有值，显示空格
+ (NSString *)showSpaceStringWithStr:(NSString *)str {
    return [self isemptyStr:str] ? @" " : str;
}

- (NSString *)finalShowStringWithSelf {
    BOOL isEmpty = [NSString isemptyStr: self];
    return isEmpty ? @" " : self;
}


-(BOOL)canUsePhoneNum{
    
    NSString *regex = @"1[34578]([0-9]){9}";
    
    return [self isValidateByRegex:regex];
    
    
}

-(BOOL)canUseEmail{
    
    NSString *regex = @"^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
    return [self isValidateByRegex:regex];
}

- (BOOL)isValidateByRegex:(NSString *)regex {
    
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [pre evaluateWithObject:self];
    
}




#pragma mark -- 设备型号
+ (NSString*)deviceString
{
    // 需要#import "sys/utsname.h"
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    
    if ([deviceString isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
    if ([deviceString isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
    if ([deviceString isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    if ([deviceString isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    if ([deviceString isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([deviceString isEqualToString:@"iPhone5,2"])    return @"iPhone 5";
    if ([deviceString isEqualToString:@"iPhone6,1"])    return @"iPhone 5S";
    if ([deviceString isEqualToString:@"iPhone3,2"])    return @"Verizon iPhone 4";
    if ([deviceString isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
    if ([deviceString isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
    if ([deviceString isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
    if ([deviceString isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
    if ([deviceString isEqualToString:@"iPod5,1"])      return @"iPod Touch 5G";
    if ([deviceString isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([deviceString isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([deviceString isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
    if ([deviceString isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([deviceString isEqualToString:@"i386"])         return @"Simulator";
    if ([deviceString isEqualToString:@"x86_64"])       return @"Simulator";
    if ([deviceString isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
    if ([deviceString isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
    if ([deviceString isEqualToString:@"iPhone8,4"])    return @"iPhone SE";
    if ([deviceString isEqualToString:@"iPhone9,2"])    return @"iPhone 7";
    if ([deviceString isEqualToString:@"iPhone9,1"])    return @"iPhone 7 Plus";
    
    //    NSLog(@"NOTE: Unknown device type: %@", deviceString);
    //    deviceString = [NSString stringWithFormat:@"%@-%@",deviceString,[NSString isJailbroken]];
    return deviceString;
}
+ (NSString *)iosVersions
{
    NSString* strVersion = [[UIDevice currentDevice] systemVersion];
    
    //    float version = [strVersion floatValue];
    NSLog(@"version:%@", strVersion);
    return strVersion;
}


+ (NSString *)IMSIString{

    CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
    
    CTCarrier *carrier = [info subscriberCellularProvider];
    
    NSString *mcc = [carrier mobileCountryCode];
    NSString *mnc = [carrier mobileNetworkCode];
    
    NSString *imsi = [NSString stringWithFormat:@"%@%@", mcc, mnc];
    
    return imsi;
}

+(NSString *)carrierNameString{
    CTTelephonyNetworkInfo *telephonyInfo = [[CTTelephonyNetworkInfo alloc] init];
    CTCarrier *carrier = [telephonyInfo subscriberCellularProvider];
//    NSString *currentCountry=[carrier carrierName];
    //先判断有没有SIM卡，如果没有则不获取本机运营商
    
    //当前手机所属运营商名称
    
    NSString *mobile;

    if (!carrier.isoCountryCode) {
        
        NSLog(@"没有SIM卡");
        
        mobile = @"无运营商";
        
    }else{
        
        mobile = [carrier carrierName];
        
    }
//    NSLog(@"[carrier isoCountryCode]==%@,[carrier allowsVOIP]=%d,[carrier mobileCountryCode=%@,[carrier mobileCountryCode]=%@",[carrier isoCountryCode],[carrier allowsVOIP],[carrier mobileCountryCode],[carrier mobileNetworkCode]);
    return mobile;
}

+ (NSString *)appCurVersion{
    
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    
    // 当前应用软件版本  比如：1.0.1
    NSString *appCurVersion1 = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSLog(@"当前应用软件版本:%@",appCurVersion1);
    
    return appCurVersion1;
}

@end
